package com.cloud.controller;

import com.cloud.entity.Goods;
import com.cloud.entity.PageInfo;
import com.cloud.entity.Warehouse;
import com.cloud.service.GoodsService;
import com.cloud.service.WarehouseService;
import com.cloud.utils.GlobalConstant;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;



@RestController
@RequestMapping(value = "/warehouse")
public class WarehouseController {

    private WarehouseService warehouseServiceImpl;
    private GoodsService goodsServiceImpl;

    public WarehouseController(WarehouseService warehouseServiceImpl,
                               GoodsService goodsServiceImpl) {
        this.warehouseServiceImpl = warehouseServiceImpl;
        this.goodsServiceImpl = goodsServiceImpl;
    }

    @PostMapping(value = "/addWarehouse")
    public int addWarehouse(Warehouse warehouse) {
        Goods goods = goodsServiceImpl.findGoodsByName(warehouse.getGoods());
        if (goods != null) {
            warehouse.setId(UUID.randomUUID().toString().replace("-", ""));
            warehouse.setTime(new Date());
            warehouse.setPrice(goods.getPrice() * warehouse.getCount());
            warehouse.setType(GlobalConstant.inWarehouse);
            int addWarehouseResult = warehouseServiceImpl.addWarehouse(warehouse);
            goods.setWarehouse(goods.getWarehouse() + warehouse.getCount());
            int updateWarehouseCount = goodsServiceImpl.updateGoods(goods);
            return addWarehouseResult == updateWarehouseCount ? GlobalConstant.SUCCESS : GlobalConstant.ERROR;
        }
        Goods good = new Goods();
        good.setName(warehouse.getGoods());
        good.setCode(UUID.randomUUID().toString().replace("-", ""));
        Date date = new Date();
        good.setType(goodsServiceImpl.findAllByPage(new PageInfo<>(0, 1), null).getData().get(0).getName());
        good.setCreater(warehouse.getOperator());
        good.setCtime(date);
        good.setUpdater(warehouse.getOperator());
        good.setUtime(date);
        good.setStatus(1);
        good.setWarehouse(warehouse.getCount());
        int addGoodsRes = goodsServiceImpl.addGoods(good);
        warehouse.setId(UUID.randomUUID().toString().replace("-", ""));
        warehouse.setPrice(0d);
        warehouse.setType(GlobalConstant.inWarehouse);
        int addWarehouseRes = warehouseServiceImpl.addWarehouse(warehouse);
        return addGoodsRes == addWarehouseRes ? GlobalConstant.SUCCESS : GlobalConstant.ERROR;
    }

    @PostMapping(value = "/outWarehouse")
    public int outWarehouse(Goods goods) {
        Goods good = goodsServiceImpl.findGoodsById(goods.getId());
        good.setWarehouse(good.getWarehouse() - goods.getWarehouse());
        int outGoodsWarehouse = goodsServiceImpl.updateGoods(good);
        Warehouse warehouse = new Warehouse();
        warehouse.setId(UUID.randomUUID().toString().replace("-", ""));
        warehouse.setGoods(good.getName());
        warehouse.setCount(goods.getWarehouse());
        warehouse.setTime(new Date());
        warehouse.setType(GlobalConstant.outWarehouse);
        warehouse.setOperator(goods.getUpdater());
        int outWarehouse = warehouseServiceImpl.addWarehouse(warehouse);
        return outGoodsWarehouse == outWarehouse ? GlobalConstant.SUCCESS : GlobalConstant.ERROR;
    }

    @GetMapping(value = "/findAllByPage")
    public PageInfo<Warehouse> findAllByPage(PageInfo<Warehouse> info,
                                             @RequestParam(value = "id", defaultValue = "") String id) {
        return warehouseServiceImpl.findAllWarehouseByPage(info, id);
    }

}
